package hxg.file;

import java.io.*;
import java.util.Properties;
import java.util.Set;

/**
 * Properties集合类的使用
 * @author HeXiangGuang
 * @create 2021-06-29 20:05
 */
public class Demo2 {
    public static void main(String[] args) throws IOException {
        //1创建集合
        Properties properties = new Properties();
        //2添加数据
        properties.setProperty("username","zhangsan");
        properties.setProperty("age","20");
        System.out.println(properties.toString());
        //3 遍历数据
        //3.1 ----KeySet----
        //3.2 ----entrySet----
        //3.3 ----StringPropertyNames----
        Set<String> pronames = properties.stringPropertyNames();
        for (String pro : pronames) {
            System.out.println(pro+"======"+properties.getProperty(pro));
        }
        //和流有关的办法
        //-- 1 list方法 --
//        PrintWriter pw = new PrintWriter("d:\\print.txt");
//        properties.list(pw);
//        pw.close();
        //-- 2 store方法 保存 --
//        FileOutputStream fos = new FileOutputStream("d:\\store.properties");
//        properties.store(fos,"注释");
//        fos.close();
        //-- 3 load方法 加载     --
        Properties properties2 = new Properties();
//        FileInputStream fis = new FileInputStream("d:\\print.txt");
        FileInputStream fis = new FileInputStream("d:\\store.properties");
        properties2.load(fis);
        fis.close();
        System.out.println(properties2.toString());
    }
}
